package com.ruike.config.Shiro;

/**
 * @author 吴泽胜
 * @FileName MyShiroRealm
 * @date 2021-02-03 6:18 下午
 * @Software: IntelliJ IDEA
 */

import com.ruike.pojo.Admin;
import com.ruike.pojo.Student;
import com.ruike.pojo.Teacher;
import com.ruike.service.AdminService;
import com.ruike.service.StudentService;
import com.ruike.service.TeacherService;
import com.ruike.utils.MD5Utils;
import lombok.SneakyThrows;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Set;

/**
 * 实现AuthorizingRealm接口用户用户认证
 */
public class UserShiroRealm extends AuthorizingRealm {

    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private AdminService adminService;

    /**
     * 执行授权逻辑
     * @param principals
     * @return
     * 设置授权信息
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("执行授权逻辑");

        /**
         * 角色
         */
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        Set<String> roles = new HashSet<String>();
        roles.add("Student");
        roles.add("Teacher");
        roles.add("Admin");
        roles.add("Super");
        authorizationInfo.setRoles(roles);

        /**
         * 角色权限集
         */
        Set<String> permissions = new HashSet<String>();

        // 学生
        permissions.add("Student:list");
        // 教师
        permissions.add("Teacher:list");
        // 教务处管理员
        // 系统管理员
        permissions.add("user:add");

        authorizationInfo.setStringPermissions(permissions);
        return authorizationInfo;
    }

    /**
     * 执行认证逻辑
     * @param authenticationToken
     * @return
     */
    @SneakyThrows
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) {
        System.out.println("执行认证逻辑");

        //  编写shiro判断逻辑，判断用户名和密码
        //  1.判断用户名
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken)authenticationToken;
        int usernameLength = usernamePasswordToken.getUsername().length();

        String password = null;
        Object object = null;

        /**
         * 老师登陆
         * Teacher POJO
         */
        if( usernameLength == 8 ){
            object = teacherService.findTeacherByteacherId( usernamePasswordToken.getUsername() );
            if ( object == null ) return null;
            password = ((Teacher) object).getPassword();
        }
        /**
         * 系统管理员登陆
         */
        else if (usernameLength == 11 ){
            object = adminService.findAdminByAdmin_id( usernamePasswordToken.getUsername() );
            if (object == null) return null;
            password = ((Admin) object).getPassword();
        }
        /**
         * 学生登录
         * Student POJO
         */
        else if (usernameLength == 13 ){
            object = studentService.findStudentIdByStudentId( usernamePasswordToken.getUsername() );
            if ( object == null ) return null;
            password = ((Student) object).getPassword();
        }
        /**
         * 超级管理员
         * Super POJO 后续添加
         */
        // 用户名不存在
        else
            return null;

        /**
         * 密码加密
         */
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(
                object,
                String.valueOf(usernamePasswordToken.getPassword()),
                ByteSource.Util.bytes( String.valueOf(usernamePasswordToken.getPassword()) + MD5Utils.getSolt() ),
                getName() );
        // 2.判断密码
        return info;
    }
}